<?php

/**
 * InvitecodeModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 10 2012-09-30 15:35:48Z rendong237@gmail.com $
 */
class InvitecodeModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["codeid"] = (int)$value["codeid"];
		$v["mid"] = (int)$value["mid"];
		$v["email"] = $value["email"];
		$v["name"] = $value["name"];
		$v["ncall"] = $value["ncall"];
		$v["enname"] = $value["enname"];
		$v["encall"] = $value["encall"];
		$v["code"] = $value["code"];
		$v["uid"] = (int)$value["uid"];
		$v["createtime"] = $value["createtime"];
		$v["usetime"] = $value["usetime"];
		
		$this->db->insert("invitecode", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($codeid, array $value)
	{
		$v = array();
		isset($value["mid"]) && $v["mid"] = (int)$value["mid"];
		isset($value["email"]) && $v["email"] = $value["email"];
		isset($value["name"]) && $v["name"] = $value["name"];
		isset($value["ncall"]) && $v["ncall"] = $value["ncall"];
		isset($value["enname"]) && $v["enname"] = $value["enname"];
		isset($value["encall"]) && $v["encall"] = $value["encall"];
		isset($value["code"]) && $v["code"] = $value["code"];
		isset($value["uid"]) && $v["uid"] = (int)$value["uid"];
		isset($value["createtime"]) && $v["createtime"] = $value["createtime"];
		isset($value["usetime"]) && $v["usetime"] = $value["usetime"];

		$this->db->update("invitecode", $v, 'codeid=' . (int)$codeid);
	}
	
	/**
	 * 删除
	 */
	public function del($codeid, $mid)
	{
		$this->db->delete("invitecode", 'codeid=' .  (int)$codeid . ' and mid=' . $mid);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($mid, $uid, $nowp, $per_page)
	{
		$where = ' where mid=' . $mid;
		if ($uid != null){
			$uid = (int)$uid;
			if ($uid == 0)
				$where .= ' and uid=0';
			else
				$where .= ' and uid!=0';
		}
		
		$totalNum = $this->db->fetchOne ('select count(*) from invitecode ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from invitecode' . $where .' order by codeid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	
	public function search($mid, $email)
	{
		$email = trim($email);
		
		if (empty($email))
			return array();

		$where = " where mid=$mid and email like '%" . $this->db->quote($email) . "%'";
		
		$list     = $this->db->fetchAll ('select * from invitecode' . $where .' order by codeid desc');
	
		return $list;
	}
	
	
	/**
	 * 显示
	 */
	public function show($codeid)
	{
		return $this->db->fetchRow("select * from `invitecode` where codeid=\"?\"", array($codeid));
	}
}